﻿/* Byte操作助手
 */

namespace LSLib.ByteEx
{
    using System.Collections.Generic;
    using System.IO;

    public static class ByteHelper
    {
        /// <summary>将Stream流转为byte数组
        /// </summary>
        /// <param name="data">Stream流数据</param>
        /// <returns>byte数组</returns>
        public static byte[] ReadAllBytesFromStream(Stream stream)
        {
            if (!stream.CanRead) return null;

            List<byte> result = new List<byte>();
            while (true)
            {
                int oneByte = stream.ReadByte();
                if (oneByte == -1) break;
                result.Add((byte)oneByte);
            }
            return result.ToArray();
        }
    }
}
